<?php

if (! empty ( $_POST )) {
	
	if (defined ( 'MLEngine' )) {
		require_once (MLEngine . 'MakiFactory/MakiFactory.php');
		
		$username = $_REQUEST ["username"];
		//$username = "tineo";
		$pass = md5 ( $_REQUEST ["pass"] );
		//$pass = md5 ( "gato" );
		

		$user = new User ();
		$user->setUsername ( $username );
		$user->setPass ( $pass );
		$factory = MakiFactory::getMakisubfactory ( MakiFactory::MYSQL );
		
		$usermaki = $factory->getUserMaki ()->validar ( $user );
		
		//echo "<br/> auth.php <br/>";
		
		if ($usermaki->getCoduser () == null) {
			print "fallo autenticacion";
			mostrar_form_login ();
		
		} else {
			
			$_SESSION ["user"] = $factory->getUserMaki ()->validar ( $user );
			
			$_SESSION ['MLPath'] = @MLPath;
			$_SESSION ['MLSources'] = @MLSources;
			$_SESSION ['MLEngine'] = @MLEngine;
			$_SESSION ['MLDealers'] = @MLDealers;
			$_SESSION ['MLTemplates'] = @MLTemplates;
			
			$mw = explode ( "/", dirname ( __FILE__ ) . "" );
			echo $mw [count ( $mw ) - 1];
			
			echo "<br/>-" . $_SERVER ['HTTP_HOST'];
			echo "<br/>-" . $_SERVER ['SCRIPT_FILENAME'];
			echo "<br/>-" . $_SERVER ['DOCUMENT_ROOT'];
			echo "<br/>-" . $_SERVER ['DOCUMENT_ROOT'] . "/" . $mw [count ( $mw ) - 1] . "/index.php";
			
			echo "1";
			
			header ( "Location: ../dashboard/ " );
		}
	} else {
		
		header ( "Location: ../dashboard/ " );
	
	}

} else { //eslse de $_post
	

	if (defined ( 'MLEngine' )) {
		mostrar_form_login ();
	} else {
		header ( "Location: ../dashboard/ " );
	}
}

function mostrar_form_login() {
	
	?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Accesso</title>
</head>
<body>

<form action="" method="post">
<div><input name="username" type="text" /></div>
<div><input name="pass" type="text" /></div>
<div><input value="OK" type="submit" /></div>
</form>

</body>
</html>


<?php
}
?>